// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.chimi.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.chimi.controller.request.QueryCardManageReq;
import com.liuxinlong.modules.chimi.controller.request.UpdateBarCardReq;
import com.liuxinlong.modules.chimi.controller.request.UpdateBarCommentReq;
import com.liuxinlong.modules.chimi.controller.request.UpdateBarStatusReq;
import com.liuxinlong.modules.chimi.service.BarManageService;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.BarCard;
import com.liuxinlong.modules.entity.BarComment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 论坛管理控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022-07-07
 */
@RestController
@RequestMapping("/bar/manage")
public class BarManageController {
    @Autowired
    private BarManageService barManageService;

    /**
     * 分页查询发帖信息
     *
     * @param req 请求信息
     * @return 发帖信息列表
     */
    @PostMapping("page")
//	@LogAnnotation(value ="发帖信息分页查询",business = LogBusinessConstants.BAR_MANAGE,operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pageCardInfo(@RequestBody QueryCardManageReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        queryParam.put("status", req.getStatus());
        queryParam.put("title", req.getTitle());
        queryParam.put("plate", req.getPlate());
        queryParam.put("createUser", req.getCreateUser());
        queryParam.put("startTime", req.getStartTime());
        queryParam.put("endTime", req.getEndTime());
        List<Map<String, Object>> dataList = barManageService.pageCard(queryParam);
        int total = barManageService.getCardCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        resp.setRetMessage("分页查询发帖信息成功");
        return resp;
    }

    /**
     * 帖子置顶
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("topping")
    @LogAnnotation(value = "置顶帖子", business = LogBusinessConstants.BAR_MANAGE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp toppingCard(@RequestBody QueryBaseReq req) {
        barManageService.toppingCard(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("置顶帖子成功");
        return resp;
    }

    /**
     * 修改帖子信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "修改帖子信息", business = LogBusinessConstants.BAR_MANAGE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateCard(@RequestBody UpdateBarCardReq req) {
        BarCard barCard = new BarCard();
        barCard.setId(req.getId());
        barCard.setReleaseType(req.getReleaseType());
        barCard.setCommentsHidden(req.getCommentsHidden());
        barCard.setCommentsModify(req.getCommentsModify());
        barCard.setTitle(req.getTitle());
        barCard.setPlate(req.getPlate());
        barCard.setContent(req.getContent());
        barCard.setAttach(req.getAttach());
        barManageService.updateCard(barCard);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改帖子信息成功");
        return resp;
    }

    /**
     * 删除帖子信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除帖子信息", business = LogBusinessConstants.BAR_MANAGE, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteCard(@RequestBody BaseDeleteReq req) {
        barManageService.deleteCard(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除帖子信息成功");
        return resp;
    }

    /**
     * 审核帖子信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("examine")
    @LogAnnotation(value = "审核帖子信息", business = LogBusinessConstants.BAR_MANAGE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp examineCard(@RequestBody UpdateBarStatusReq req) {
        barManageService.examineCard(req.getId(), req.getResult(), req.getOpinion(), req.getSyncMessage(), req.getFineFlag());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("审核帖子信息成功");
        return resp;
    }

    /**
     * 帖子点赞信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("thumb")
    public BaseResp pageCardThumb(@RequestBody QueryBaseReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("businessId", req.getId());
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        List<Map<String, Object>> dataList = barManageService.pageCardThumb(queryParam);
        int totalNum = barManageService.getCardThumbCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        resp.setRetMessage("查询帖子点赞信息成功");
        return resp;
    }


    /**
     * 分页查询评论信息
     *
     * @param req 请求信息
     * @return 评论信息列表
     */
    @PostMapping("/comment/page")
//	@LogAnnotation(value ="评论信息分页查询",business = LogBusinessConstants.BAR_MANAGE,operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pageCommentInfo(@RequestBody QueryCardManageReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        queryParam.put("cardId", req.getCardId());
        queryParam.put("status", req.getStatus());
        queryParam.put("title", req.getTitle());
        queryParam.put("createUser", req.getCreateUser());
        queryParam.put("startTime", req.getStartTime());
        queryParam.put("endTime", req.getEndTime());
        List<Map<String, Object>> dataList = barManageService.pageComment(queryParam);
        int total = barManageService.getCommentCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        resp.setRetMessage("分页查询评论信息成功");
        return resp;
    }

    /**
     * 评论置顶
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("/comment/topping")
    @LogAnnotation(value = "置顶评论", business = LogBusinessConstants.BAR_MANAGE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp toppingComment(@RequestBody BaseDeleteReq req) {
        barManageService.toppingComment(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("置顶评论成功");
        return resp;
    }

    /**
     * 修改评论信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("/comment/update")
    @LogAnnotation(value = "修改评论信息", business = LogBusinessConstants.BAR_MANAGE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateComment(@RequestBody UpdateBarCommentReq req) {
        BaseResp resp = new BaseResp();
        BarComment barComment = new BarComment();
        barComment.setId(req.getId());
        barComment.setContent(req.getContent());
        barComment.setAttach(req.getAttach());
        barManageService.updateComment(barComment);
        resp.setRetMessage("修改评论信息成功");
        return resp;
    }

    /**
     * 删除评论信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("/comment/delete")
    @LogAnnotation(value = "删除评论信息", business = LogBusinessConstants.BAR_MANAGE, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteComment(@RequestBody BaseDeleteReq req) {
        barManageService.deleteComment(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除评论信息成功");
        return resp;
    }

    /**
     * 审核评论信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("/comment/examine")
    @LogAnnotation(value = "审核评论信息", business = LogBusinessConstants.BAR_MANAGE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp examineComment(@RequestBody UpdateBarStatusReq req) {
        barManageService.examineComment(req.getId(), req.getResult());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("审核评论信息成功");
        return resp;
    }

    /**
     * 导出用户发帖信息
     *
     * @return 导出数据
     */
    @PostMapping("export")
    @LogAnnotation(value = "导出发帖信息", business = LogBusinessConstants.BAR_MANAGE, operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportCardInfo(@RequestBody QueryCardManageReq req,HttpServletResponse response) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("status", req.getStatus());
        queryParam.put("title", req.getTitle());
        queryParam.put("createUser", req.getCreateUser());
        queryParam.put("startTime", req.getStartTime());
        queryParam.put("endTime", req.getEndTime());
        barManageService.exportCardInfo(queryParam,response);
    }

    /**
     * 导出用户评论信息
     *
     * @return 导出数据
     */
    @PostMapping("comment/export")
    @LogAnnotation(value = "导出评论信息", business = LogBusinessConstants.BAR_MANAGE, operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportCommentInfo(@RequestBody QueryCardManageReq req, HttpServletResponse response) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("cardId", req.getCardId());
        queryParam.put("status", req.getStatus());
        queryParam.put("title", req.getTitle());
        queryParam.put("createUser", req.getCreateUser());
        queryParam.put("startTime", req.getStartTime());
        queryParam.put("endTime", req.getEndTime());
        barManageService.exportCommentInfo(queryParam, response);
    }
}
